
const dayjs = require("dayjs");
const { addRecord, queryList, updateRecord } = require("../controller/apiconfig-controller");
const { serverError } = require("../utils");
const { emptyCheck } = require("../utils/checkEmpty");
const pageSizeList = require("../utils/slicePageSize");

/**
 * @description 访客统计路由
 * @author 韩
 */
const router = require("koa-router")()
router.prefix("/apiconfig");

// 添加或编辑
router.post("/add", async (ctx, next) => {
    try {

        const { createDate = dayjs(),
            title = null, config = null, status = null } = ctx.request.body
        const check = emptyCheck({ title, config, status })
        if (check.code!== 0) {
            ctx.body = check
        } else {
            const res = await addRecord({ createDate, title, config, status })
            ctx.body = res
        }
    } catch (error) {
        console.log(error)
        ctx.body = serverError
    }
})

// // 查询列表
router.post("/list", async (ctx, next) => {
    try {
        const { current, pageSize, title } = ctx.request.body;
        const res = await queryList({
            title
        })
        ctx.body = {
            status: "0000",
            message: "查询成功",
            data: {
                total: res.length,
                list: pageSizeList({ data: res, pageSize, current })
            }
        }
    } catch (error) {
        console.log(error)
        ctx.body = serverError
    }
})

router.post("/update", async (ctx, next) => {
    try {
        const { _id, config, title, status } = ctx.request.body;
        const res = await updateRecord({ _id, config, title, status })
        console.log('update路由', res);
        ctx.body=res;
    } catch (error) {
        console.log(error)
        ctx.body = serverError
    }
})


// // 删除
// router.post("/delete", async (ctx, next) => {
//     try {
//         const { id } = ctx.request.body
//         const check = emptyCheck({ id })
//         if (check.code!== 0) {
//             ctx.body = check;
//         }
//         else {
//             const res = await deltetArticle({ _id: id })
//             ctx.body = res;
//         }
//     } catch (error) {
//         ctx.body = serverError
//     }
// })

// // 查看专题详情
// router.post("/detail", async (ctx, next) => {
//     try {
//         const { _id } = ctx.request.body
//         const check = emptyCheck({ _id })
//         if (check.code!== 0) {
//             ctx.body = check;
//         } else {
//             const res = await getSubjectDetail({ _id: _id })
//             ctx.body = res;
//         }

//     } catch (error) {
//         ctx.body = serverError
//     }
// })

module.exports = router